package com.paomi.client.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.ViewGroup;

import com.youth.banner.Banner;

/**
 * Created by pc on 2018/8/13
 * Describe:PtrFrameLayout与ViewPager的滑动冲突
 * cn.stareal.stareal.Util
 */
public class HorizontalBanner extends Banner {
    private ViewGroup mPtrLayout;
    private float mDownX;
    private float mDownY;
    public HorizontalBanner(Context context){
        super(context);
    }
    public HorizontalBanner(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
    }

    public HorizontalBanner(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
    }
    public void setNestedpParent(ViewGroup parent) {
        this.mPtrLayout = parent;
    }

    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()) {
            case MotionEvent.ACTION_DOWN:
                getParent().requestDisallowInterceptTouchEvent(true);
                if(mPtrLayout!=null){
                    mPtrLayout.setEnabled(false);
                }

                break;
            case MotionEvent.ACTION_MOVE:
                int moveX = (int) ev.getX();
                int moveY = (int) ev.getY();
                //ViewPager滑动
                if (Math.abs(moveX-mDownX) > Math.abs(moveY-mDownY)) {
                    getParent().requestDisallowInterceptTouchEvent(true);
                    if(mPtrLayout!=null){
                        mPtrLayout.setEnabled(false);
                    }
                    //PtrFrameLayout滑动
                }else {
                    getParent().requestDisallowInterceptTouchEvent(false);
                    if(mPtrLayout!=null){
                        mPtrLayout.setEnabled(true);
                    }
                }
                mDownX = moveX;
                mDownY = moveY;
                break;
            case MotionEvent.ACTION_CANCEL:
            case MotionEvent.ACTION_UP:
                if(mPtrLayout!=null){
                    mPtrLayout.setEnabled(true);
                }
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

}
